﻿package net.steam.easyjoboa.web.action;

import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import net.steam.easyjoboa.biz.ProclaimBiz;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.PageBean;
import net.steam.easyjoboa.entity.Proclaim;

public class ProclaimAction {

	private ProclaimBiz proclaimBiz;
	private Employee employee;

	/**
	 * 发布公告
	 * @return
	 */
	private Proclaim proclaim;

	@Action(value = "send_proclaim", results = {
			@Result(name = "input", location = "/send_proclaim.jsp"),
			@Result(name = "success", type = "redirectAction", location = "getAllProclaims") })
	public String send_proclaim() {

		proclaim.setStatus(0);// 设置公告的状态，0表示有效，1表示过期。默认是0
		// 公告是谁发布的，拿取登录之后的用户。只有是部门经理和管理员才发布
		proclaim.setEmployee((Employee) ServletActionContext.getRequest().getSession()
				.getAttribute("loginUser"));
		proclaim.setCreatetime(new Date());
		int key = proclaimBiz.addProclaim(proclaim);
		if (key == 0) {
			return "input";
		} else {
			return "success";
		}
	}

	/**
	 * 管理员查看所有的公告
	 */
	private List<Proclaim> proclaims;
	// 接收用户的职位名称，判断页面是否可以修改
	private String name;
	private PageBean pb;

	@Action(value = "getAllProclaims", results = {
			@Result(name = "input", location = "/proclaim_list.jsp"),
			@Result(name = "success", location = "/proclaim_list.jsp") })
	public String getAllProclaims() {
		employee = (Employee) ServletActionContext.getRequest().getSession()
				.getAttribute("loginUser");
		name = employee.getPosition().getPositionName();
		if (pb == null) {
			pb = new PageBean();
		}
		// 设置每页10条记录
		pb.setPageSize(10);
		proclaims = proclaimBiz.findAllProclaim(pb);
		if (proclaims.size() == 0) {
			proclaims = null;
			return "input";
		} else {
			return "success";
		}
	}

	/**
	 * 员工查看有效公告
	 * @return
	 */
	@Action(value = "seeProclaim", results = {
			@Result(name = "input", location = "/proclaim_see.jsp"),
			@Result(name = "success", location = "/proclaim_see.jsp") })
	public String seeProclaim() {
		employee = (Employee) ServletActionContext.getRequest().getSession()
				.getAttribute("loginUser");
		name = employee.getPosition().getPositionName();
		if (pb == null) {
			pb = new PageBean();
		}
		// 设置每页10条记录
		pb.setPageSize(10);
		proclaims = proclaimBiz.seeProclaim(pb);
		if (proclaims.size() == 0) {
			proclaims = null;
			return "input";
		} else {
			return "success";
		}
	}

	/**
	 * 根据ID 获取公告的详细内容
	 */
	private int id;

	@Action(value = "getDetail", results = { @Result(name = "input", location = "/error.jsp"),
			@Result(name = "success", location = "/proclaim_detail.jsp") })
	public String getDetail() {
		proclaim = proclaimBiz.findProclaimById(id);
		if (proclaim == null) {
			return "input";
		} else {
			return "success";
		}

	}

	/**
	 * 查询并且跳转到修改页面
	 * 
	 * @return
	 */
	@Action(value = "updateProclaim", results = { @Result(name = "input", location = "/error.jsp"),
			@Result(name = "success", location = "/update_proclaim.jsp") })
	public String updateProclaim() {

		proclaim = proclaimBiz.findProclaimById(id);
		if (proclaim == null) {
			return "input";
		} else {
			return "success";
		}
	}

	/**
	 * 执行更新公告
	 * @return
	 */
	private String status;
	private String title;
	private String content;

	@Action(value = "update", results = { @Result(name = "success", type = "redirect", location = "getAllProclaims") })
	public String update() {
		proclaim = proclaimBiz.findProclaimById(id);
		proclaim.setContent(content);
		proclaim.setTitle(title);
		proclaim.setStatus(Integer.parseInt(status));
		proclaimBiz.updateProclaim(proclaim);
		return "success";
	}

	public Proclaim getProclaim() {
		return proclaim;
	}

	public void setProclaim(Proclaim proclaim) {
		this.proclaim = proclaim;
	}

	public void setProclaimBiz(ProclaimBiz proclaimBiz) {
		this.proclaimBiz = proclaimBiz;
	}

	public List<Proclaim> getProclaims() {
		return proclaims;
	}

	public void setProclaims(List<Proclaim> proclaims) {
		this.proclaims = proclaims;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public PageBean getPb() {
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}
}
